<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="AssetList.aspx.cs" Inherits="AssetList" Title="ABC SuperSign - Asset List" %>
<%@ Register Src="UserAccess.ascx" TagName="UserAccess" TagPrefix="uc1" %>
<%@ Register Src="GlobalAjax.ascx" TagName="GlobalAjax" TagPrefix="uc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Main" Runat="Server">
<form id="Form_AssetList" runat="server"> 
<asp:HiddenField ID="Hidden_BtnChoice" runat="server" />    
<asp:ScriptManager ID="ScriptManager_AssetList" runat="server">
</asp:ScriptManager> 

<uc2:GlobalAjax ID="ctl_GlobalAjax" runat="server" />

<!-- page title start -->
<h1>
    <span>Asset List</span>
    <asp:Button ID="Btn_New" runat="server" Text="Create New Asset" CssClass="aspButton" PostBackUrl="~/Editor.aspx" OnClick="Btn_New_Click" />
</h1>
<!-- page title end -->
<!-- Main Content start -->
<div class="mainHolder">
<!-- left block start -->
<div class="leftBlock">
    <h2>
	    <span>Search Assets</span>
    </h2>
	<div class="innerContent">
	<asp:Panel ID="Panel1" runat="server" DefaultButton="Btn_Search" >
        <div id="assetList_SearchFields"></div>
	    <div id="assetSearchButtons" class="row" style="display:none;">
        <asp:Button ID="Btn_Search" runat="server" Text="Search" OnClientClick="prepareSearch();" OnClick="Btn_Search_Click" />
        <div class="spacerS"></div>
        <input id="assetReset" type="button" value="Reset" onclick="resetSearchFields('assetList_SearchFields');"/>        
    </div>
     </asp:Panel>
    </div>
</div>
<!-- left block end -->
<!-- right block start -->
<div class="rightBlock">
    <h2>
	    <span>Search Results</span>
    </h2>
    <div class="innerContent">
    <asp:UpdatePanel ID="UpdatePanel_Alerts" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Btn_Search" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <div class="row">
                <asp:Label ID="Label_AssetAlerts" runat="server" Text=""></asp:Label>
        </div>
    </ContentTemplate>
    </asp:UpdatePanel>
    <div class="row">
    <asp:UpdatePanel ID="UpdatePanel_Details" runat="server" UpdateMode="Conditional">
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Btn_Search" EventName="Click"></asp:AsyncPostBackTrigger>
        <asp:AsyncPostBackTrigger ControlID="DDList_Sort" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
        </Triggers>   
    <ContentTemplate>
        <div class="col widthXM">
            <div class="row">
                <label class="big">
                    <span>Assets</span>
                    <asp:DropDownList ID="DDList_Sort" runat="server" AutoPostBack="true" CssClass="small right"
                        OnSelectedIndexChanged="DDList_Sort_SelectedIndexChanged">
                        <asp:ListItem Value="0" Text="Sort By" />
                        <asp:ListItem Value="1" Text="Name" />
                        <asp:ListItem Value="2" Text="Newest" />
                    </asp:DropDownList>
                </label>
            </div>
            <div class="row">
                <asp:ListBox ID="ListBox_Results" runat="server" CssClass="heightL widthXM"></asp:ListBox>
            </div>
        </div>
        <div class="spacerXL">
        </div>
        <div class="col">
            <div class="row">
                <label class="big">
                    Asset Info</label>
            </div>
            <div class="row">
                <asp:Panel ID="Panel_AssetInfo" runat="server" CssClass="aspPanel heightM widthXM">
                    <asp:Label ID="NameLabel" runat="server" Text=""></asp:Label>
                </asp:Panel>
            </div>
        </div>
    </ContentTemplate> 
    </asp:UpdatePanel>
	    <div class="spacerXL"></div>
	    <div class="col">
	        <div class="row">
		        <label>&nbsp;</label>
		    </div>
		    <div class="row">
		    <asp:Button ID="Btn_Edit" runat="server" Text="Edit Selected Asset" CssClass="widthM" PostBackUrl="Editor.aspx" OnClick="Btn_Edit_Click" />
		    </div>
            <div id="btnDuplicateRow" class="row" style="display:none;">
                <input type="button" id="btnDuplicate" disabled="disabled" class="widthM" value="Duplicate Asset As..." onclick="prepareDuplicateAs();" />
            </div>
            <div id="btnDisableRow" class="row" style="display:none;">
                <input type="button" id="btnDisable" disabled="disabled" class="widthM" value="Disable Asset" onclick="prepareDisable();" />
            </div>
            <div id="btnEnableRow" class="row" style="display:none;">
                <input type="button" id="btnEnable" disabled="disabled" class="widthM" value="Enable Asset" onclick="prepareEnable();" />
            </div>
<%--            <div class="row">
                <input type="text" id="escapeText" />
            </div>
            <div class="row">
                <input type="button" id="btnEscape" class="widthM" value="Submit Escape" onclick="submitEscape();" />
            </div>--%>
	    </div>
    </div>
    </div>
</div>
<!-- right block end -->
</div>
<!-- main content end -->

<script type="text/javascript">
    document.onclick =  function () {
            $('<%= Label_AssetAlerts.ClientID %>').style.display = "none";
        }
        
//    document.onkeypress = function (e) {
//        if (e.keyCode == 13) {
//            prepareSearch();
//            __doPostBack('<%= Btn_Search.ClientID %>','');
//        }
//    }
    
    function submitEscape() {
        var request = new XMLHttpRequest();
        var myText = EscapeChar($('escapeText').value.strip());
        // create xml
        var xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
        xmlStr += "<ROOT>";
        xmlStr += "<NODE MyText=\"" + myText + "\" />";
        xmlStr += "</ROOT>";
        var url = "DecodeAjaxPost.aspx";
        request.open('POST', url, false);
        request.setRequestHeader("Content-Type", "text/xml");
        //var escapedXML = EscapeChar(xmlStr);
        request.send(xmlStr);
        
    }
    
    function EscapeChar(strInput)
    {
      // replace special characters that will error out in xml
      strInput=strInput.replace(/&/g,"&amp;");  //replace & with &amp;
      strInput=strInput.replace(/</g,"&lt;");	//replace < with &lt;
      strInput=strInput.replace(/>/g,"&gt;");	//replace > with &gt;
      strInput=strInput.replace(/"/g,"&quot;");	//replace " with &quot;
      strInput=strInput.replace(/'/g,"&apos;");	//replace ' with &apos;      
      return(strInput);
    }
    
    function loadAssetSearchFields () {
    	
        // make calls to load search params                           
        var sProc = "<STOREDPROCEDURE Name=\"sproc_WEB_GetSearchFields\" Level=\"4\" />";
        callAjaxPost(comboSFRequest, sProc, processComboSFRequestChange);   
    }
    activeEditorPageId = "assetList";
    loadAssetSearchFields(); 
    
    // event handler for index change on selected listbox
    function selectedIndexChange() {
        var selectList = $('<%= ListBox_Results.ClientID %>');
        // request info for selected item
        if (selectList.selectedIndex < 0) return;
        requestGetInfo(selectList.options[selectList.selectedIndex].value, 4, $('<%= Panel_AssetInfo.ClientID %>'));
        if (selectList.selectedIndex != -1) {
            $('<%= Btn_Edit.ClientID %>').removeAttribute('disabled');
            $('btnDuplicate').removeAttribute('disabled');
            if (selectList.options[selectList.selectedIndex].hasClassName('disabledOption')) {
                $('btnDisable').setAttribute('disabled', 'disabled');
                $('btnEnable').removeAttribute('disabled');
            } else {
                $('btnDisable').removeAttribute('disabled');
                $('btnEnable').setAttribute('disabled', 'disabled');
            }
        } 
    }
    
    // event handler for search click
    function searchClick() {
        $('<%= Btn_Edit.ClientID %>').setAttribute('disabled', 'disabled');
        $('btnDuplicate').setAttribute('disabled', 'disabled');
        $('btnDisable').setAttribute('disabled', 'disabled');
        $('btnEnable').setAttribute('disabled', 'disabled');
    }
    
    // function to prepare duplicate as popup
	function prepareDuplicateAs() {
	    var selectList = $('<%= ListBox_Results.ClientID %>');
        var name = selectList.options[selectList.selectedIndex].text;
        var dbId = selectList.options[selectList.selectedIndex].value;
        var msgNode = $('<%= Label_AssetAlerts.ClientID %>');
        $('dupAlert').style.display = "none";
	    showDuplicateAs('asset', name, dbId, msgNode);
	}   
	
	// function to prepare call to disable object
	function prepareDisable() {
	    var selectList = $('<%= ListBox_Results.ClientID %>');
        var dbId = selectList.options[selectList.selectedIndex].value;
        var msgNode = $('<%= Label_AssetAlerts.ClientID %>');
	    requestEnableDisable(dbId, 4, false, msgNode)
	}
	
	// function to prepare call to enable object
	function prepareEnable() {
	    var selectList = $('<%= ListBox_Results.ClientID %>');
        var dbId = selectList.options[selectList.selectedIndex].value;
        var msgNode = $('<%= Label_AssetAlerts.ClientID %>');
	    requestEnableDisable(dbId, 4, true, msgNode)
	}	        

	// function to prepare search
	var assetSearchRequest = null;
	function prepareSearch() {
	    assetSearchRequest = new XMLHttpRequest();
        $('pageContainer').addClassName('fadedBG');
        $('searchingMsg').style.display = "block";
        // add values to search xml 
        addSearchValuesToXML();  

        var url = "ListSearchPost.aspx";
        assetSearchRequest.open('POST', url, false);
        assetSearchRequest.setRequestHeader("Content-Type", "text/xml");
        assetSearchRequest.send(sfXML);
        if (assetSearchRequest.readyState == 4) {
		    if (assetSearchRequest.status == 200) {
		        $('pageContainer').removeClassName('fadedBG');
	            $('searchingMsg').style.display = "none";
		        return;
		    }
		}
	}

</script>
<uc1:UserAccess ID="UserAccess2" runat="server" />
</form>
</asp:Content>

